﻿using Abp.Domain.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace MyCompanyName.AbpZeroTemplate.Prm.Admin
{
    public class OperationLogManager : AbpZeroTemplateDomainServiceBase
    {
        public IRepository<OperationLog, int> operationLogRepository { get; private set; }

        public OperationLogManager(IRepository<OperationLog, int> _operationLogRepository)
        {
            operationLogRepository = _operationLogRepository;
        }

        public async Task Create(int hoseId, string note, HostOperation operation, string parameters, LogType lt = LogType.Other)
        {
            string clientName = Environment.MachineName;
            string ip = GetAddressIP();
            string browser = HttpContext.Current.Request.Browser.Browser;
            string platform = HttpContext.Current.Request.Browser.Platform;
            await operationLogRepository.InsertAsync(new OperationLog()
            {
                BrowserInfo = browser,
                ClientIpAddress = ip,
                ClientName = clientName,
                HostId = hoseId,
                Note = note,
                Operation = operation,
                Parameters = parameters,
                Platform = platform
            });
        }

        public void FastCreate(int hoseId, string note, HostOperation operation, string parameters)
        {
            string clientName = Environment.MachineName;
            string ip = GetAddressIP();
            string browser = HttpContext.Current.Request.Browser.Browser;
            string platform = HttpContext.Current.Request.Browser.Platform;
            operationLogRepository.Insert(new OperationLog()
           {
               BrowserInfo = browser,
               ClientIpAddress = ip,
               ClientName = clientName,
               HostId = hoseId,
               Note = note,
               Operation = operation,
               Parameters = parameters,
               Platform = platform
           });
        }


        public string GetAddressIP()
        {
            ///获取本地的IP地址
            string AddressIP = string.Empty;
            foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
            {
                if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
                {
                    AddressIP = _IPAddress.ToString();
                }
            }
            return AddressIP;
        }
    }
}
